package com.ut.richpath.util;

import com.ut.richpath.pathparser.PathDataNode;
import ohos.agp.render.Path;
import ohos.agp.render.Region;
import ohos.agp.utils.Matrix;
import ohos.agp.utils.Rect;
import ohos.agp.utils.RectFloat;

import java.util.List;

public class PathUtils {

    public static void resizePath(Path path, float width, float height){
        RectFloat src = new RectFloat();
//        path.computeBounds(src, true)
        path.computeBounds(src);
        Matrix resizeMatrix = new Matrix();
        RectFloat bounds = new RectFloat(0f, 0f, width, height);
        resizeMatrix.setRectToRect(src, bounds, Matrix.ScaleToFit.FILL);
        path.transform(resizeMatrix);
    }

    public static void setPathWidth(Path path, float width){
        RectFloat src = new RectFloat();
        path.computeBounds(src);
        Matrix resizeMatrix = new Matrix();
        RectFloat bounds = new RectFloat(src.left, src.top, src.left + width, src.bottom);
        resizeMatrix.setRectToRect(src, bounds, Matrix.ScaleToFit.FILL);
        path.transform(resizeMatrix);
    }

    public static void setPathHeight(Path path, float height){
        RectFloat src = new RectFloat();
        path.computeBounds(src);
        Matrix resizeMatrix = new Matrix();
        RectFloat bounds = new RectFloat(src.left, src.top, src.right, src.top + height);
        resizeMatrix.setRectToRect(src, bounds, Matrix.ScaleToFit.FILL);
        path.transform(resizeMatrix);
    }

    public static float getPathWidth(Path path){
        RectFloat rect = new RectFloat();
        path.computeBounds(rect);
        return rect.getWidth();
    }

    public static float getPathHeight(Path path){
        RectFloat rect = new RectFloat();
        path.computeBounds(rect);
        return rect.getHeight();
    }

    public static void setPathRotation(Path path, Float rotation){
        RectFloat rect = new RectFloat();
        path.computeBounds(rect);
        setPathRotation(path, rotation, rect.getCenter().getPointX(), rect.getCenter().getPointY());
    }

    public static void setPathRotation(Path path, float rotation, float px, float py){
        Matrix  matrix = new Matrix();
        matrix.setRotate(rotation, px, py);
        path.transform(matrix);
    }

    public static void setPathTranslationX(Path path, float translationX){
        Matrix  matrix = new Matrix();
        matrix.postTranslate(translationX, 0f);
        path.transform(matrix);
    }

    public static void setPathTranslationY(Path path, float translationY){
        Matrix  matrix = new Matrix();
        matrix.setTranslate(0f, translationY);
        path.transform(matrix);
    }

    public static void setPathScaleX(Path path, float scale){
        RectFloat rect = new RectFloat();
        path.computeBounds(rect);
        setPathScaleX(path, scale, rect.getCenter().getPointX(), rect.getCenter().getPointY());
    }

    public static void setPathScaleX(Path path, float scale, float px, float py){
        Matrix  matrix = new Matrix();
        matrix.setScale(scale, 1f, px, py);
        path.transform(matrix);
    }

    public static void setPathScaleY(Path path, float scale){
        RectFloat rect = new RectFloat();
        path.computeBounds(rect);
        setPathScaleY(path, scale, rect.getCenter().getPointX(), rect.getCenter().getPointY());
    }

    public static void setPathScaleY(Path path, float scale, float px, float py){
        Matrix  matrix = new Matrix();
        matrix.setScale(1f, scale, px, py);
        path.transform(matrix);
    }

    public static void setPathDataNodes(Path path, List<PathDataNode> pathDataNodes){
        path.reset();
        PathDataNode.nodesToPath(pathDataNodes, path);
    }

    public static boolean isTouched(Path path, float x, float y){
        RectFloat rectF = new RectFloat();
        path.computeBounds(rectF);
        Region region = new Region();
        region.setPath(path, new Region(new Rect((int)rectF.left, (int)rectF.top, (int)rectF.right, (int)rectF.bottom)));
        int offset = 10;
        return (region.contains((int)x, (int)y)
                || region.contains((int)x + offset, (int)y + offset)
                || region.contains((int)x + offset, (int)y - offset)
                || region.contains((int)x - offset, (int)y - offset)
                || region.contains((int)x - offset, (int)y + offset));
    }

}
